package cn.ilikes.tools.nosql.jpa.clients.indexs.index.hadoops;

import java.io.IOException;


import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.lucene.store.Lock;

public class HadoopLock extends Lock {

	private Path lockName;

	private FileSystem fileSystem;

	public  HadoopLock(FileSystem fileSystem, Path dirs, String name) {
		Path lockName = new Path(dirs, name);
		this.lockName = lockName;
		this.fileSystem = fileSystem;
	}

	@Override
	public boolean isLocked() throws IOException {
                    return fileSystem.exists(lockName);
		
	}

	@Override
	public boolean obtain() throws IOException {
		 if(fileSystem.exists(lockName))
		 {
			 return false;
		 }else
		 {
			return  fileSystem.createNewFile(lockName);
		 }
	}

	@Override
	public void release() throws IOException {
		fileSystem.delete(lockName,false);
	}

}
